<template>
  <div class="category student-archives">
    <div style="display: flex">
      <el-card class="box-card" title="分类">
        <el-scrollbar class="category-left">
          <el-tree
              ref="tree"
              :data="categoryTree"
              node-key="id"
              :expand-on-click-node="false"
              :highlight-current="true"
              @node-click="handleClick"
          ></el-tree>
        </el-scrollbar>
      </el-card>
      <el-card style="flex: 1">
        <div class="clearfix">
          <el-input v-model="title" placeholder="请输入标题"/>
          <el-button type="primary" style="margin-left: 10px" @click="listByTitle">搜索</el-button>
        </div>
        <el-row id="artList" type="flex" justify="space-around" v-for="item of articleList" :key="item.id">
          <el-col>
            <el-row class="art-item">
              <el-card shadow="hover">
                <h5>
                  <router-link :to="'/article/' + item.id" tag="span" class="art-title">{{
                      item.title
                    }}
                  </router-link>
                </h5>
                <el-row class="art-info d-flex align-items-center justify-content-start">
                  <div class="art-time">
                    <i class="el-icon-time"/>：{{ item.createTime }}
                  </div>
                  <div class="d-flex align-items-center">
                    <i class="iconfont icon-24gf-tags2"/>：
                    <span class="art-tag" v-for="tagItem of item.blogTagList" :key="tagItem.id"><el-tag
                        size="mini">{{
                        tagItem.name
                      }}</el-tag></span>
                  </div>
                </el-row>
                <el-row class="art-body">
                  <div class="side-img hidden-sm-and-down">
                    <router-link :to="'/article/' + item.id"><img class="art-banner"
                                                                  :src="item.avatar"></router-link>
                  </div>
                  <div class="side-abstract">
                    <div class="art-abstract">
                      {{ replaceHtml(item.content) }}
                    </div>
                    <div class="art-more">
                      <router-link :to="'/article/' + item.id" tag="span">
                        <el-button plain>阅读全文</el-button>
                      </router-link>
                      <div class="view"><i class="el-icon-view"/>{{ item.quantity }}</div>
                    </div>
                  </div>
                </el-row>
                <i class="iconfont icon-top01 star"></i>
              </el-card>
            </el-row>
          </el-col>
        </el-row>
        <div class="block pagination">
          <el-pagination background layout="prev, pager, next" :current-page="currentPage"
                         :page-size="pageSize"
                         :total="total" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>
<script>
import {listArticle, listCategoryTree} from "@/api";
import {replaceHtml} from "@/views/article";

export default {
  name: 'cate',
  data() {
    return {
      title: null,
      nodeId: 0,
      articleList: [],
      pageSize: 5,
      currentPage: 1,
      total: 0,
      categoryTree: [],
    }
  },
  watch: {
    '$route'() {
      this.listCategoryTree();
    }
  },
  mounted() {
    this.listCategoryTree();
  },
  methods: {
    listCategoryTree() {
      const categoryId = this.$route.params.categoryId;
      listCategoryTree().then(res => {
        this.categoryTree = res.data;
        if (!categoryId) {
          this.nodeId = this.categoryTree[0].id;
        } else {
          this.nodeId = categoryId;
        }
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.nodeId);
        });

        // 查询文章
        this.listArticle();
      });
    },
    listArticle() {
      listArticle({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        categoryId: this.nodeId,
        title: this.title
      }).then(res => {
        this.articleList = res.data.results;
        this.currentPage = res.data.pageNum;
        this.total = res.data.total;
      });
    },
    handleClick(node) {
      this.nodeId = node.id;
      this.pageSize = 5;
      this.currentPage = 1;
      this.$router.push("/category/" + this.nodeId);
    },
    listByTitle() {
      this.pageSize = 5;
      this.currentPage = 1;
      this.listArticle();
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      this.listArticle();
    },
    // html只保留文本
    replaceHtml(val) {
      return replaceHtml(val);
    }
  }
}
</script>
<style scoped="scoped">
.category {
  margin: 20px 0;
  height: auto;
  min-height: 830px;
}

.box-card {
  width: 250px;
  margin-right: 10px;
  height: auto;
}

.category-left {
  min-height: 830px;
}

.clearfix {
  display: flex;
  margin: 20px 0;
  width: 400px;
}

.art-item {
  margin-bottom: 20px;
  position: relative;
}

.art-title {
  border-left: 3px solid #F56C6C;
  padding-left: 5px;
  cursor: pointer;
}

.art-title:hover {
  padding-left: 10px;
  color: #409EFF;
}

.art-time {
  margin-right: 20px;
}

.art-body {
  display: flex;
  padding: 10px 0;
}

.side-img {
  height: 150px;
  width: 270px;
  overflow: hidden;
  margin-right: 10px;
}

.art-item .star {
  font-size: 20px;
  width: 60px;
  height: 60px;
  position: absolute;
  color: orangered;
  top: 2%;
  right: 0;
}

img.art-banner {
  width: 100%;
  height: 100%;
  transition: all 0.6s;
}

img.art-banner:hover {
  transform: scale(1.4);
}

.side-abstract {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.art-abstract {
  flex: 1;
  color: #aaa;
}

.art-more {
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.art-more .view {
  color: #aaa;
}

.art-info {
  margin-top: 20px;
  display: flex;
}

.art-tag {
  margin-left: 5px;
}

h5 {
  font-size: 18px;
}

.pagination {
  margin-top: 20px;
}
</style>

<style lang="scss">
.student-archives {
  .el-scrollbar__wrap {
    height: auto;
  }

  .el-tree-node__content {
    height: 40px;
  }
}
</style>
